package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.home.CalculatorAc;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.PoolCoinDetailsBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TimeCountUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.StopPayDialog;
import yt.kpool.view.dialog.SuccessDialog;
import yt.kpool.view.pop.CurrencyPopupWindow;

/**
 * 付款设置详情
 */
public class PaySetDetailAc extends AppCompatActivity {

    @BindView(R.id.tv_verificationcode)
    TextView tv_verificationcode;
    @BindView(R.id.tv_currrncyname)
    TextView tv_currrncyname;
    @BindView(R.id.iv_curreney)
    ImageView iv_curreney;
    @BindView(R.id.ly_noset)
    LinearLayout ly_noset;
    @BindView(R.id.ly_set)
    LinearLayout ly_set;
    @BindView(R.id.ll_currency)
    LinearLayout ll_currency;

    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_cointype)
    TextView tv_cointype;
    @BindView(R.id.tv_addresstype)
    TextView tv_addresstype;
    @BindView(R.id.edit_address)
    EditText edit_address;
    @BindView(R.id.edit_code)
    EditText edit_code;
    private TimeCountUtil timeCountUtil;//时间计时

    private List<CurrencyList.DataBean> currencyListData;
    private CurrencyPopupWindow currencyPopupWindow;//币种选择
    private LoadingDialog loadingDialog;
    private int currencyId;//币种id
    private String currencycode = "";//币种code

    public static void start(Activity mactivity, String ChildAccountID, String ChildAccountName) {
        Intent intent = new Intent(mactivity, PaySetDetailAc.class);
        intent.putExtra("ChildAccountID", ChildAccountID);
        intent.putExtra("ChildAccountName", ChildAccountName);

        mactivity.startActivity(intent);
    }

    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }

    public String getChildAccountName() {
        return getIntent().getStringExtra("ChildAccountName");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paysetdetail);
        ButterKnife.bind(this);
        init();
        if(NetWorkUtil.checkNet(PaySetDetailAc.this))
        {
            getCurrrencyData();
        }
        else
        {

            ToastUtil.showToast(PaySetDetailAc.this,getResources().getString(R.string.internet_error));
        }

    }

    private void init() {
        timeCountUtil = new TimeCountUtil(PaySetDetailAc.this, 60000, 1000, tv_verificationcode, getResources().getColor(R.color.system_color));
        loadingDialog = new LoadingDialog(this, 0);
        tv_name.setText(getChildAccountName());
    }


    /**
     * 获取币种数据
     */
    private void getCurrrencyData() {
        currencyListData = new ArrayList<>();
        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.GetPayCurrencyList(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {

                    Log.i("-->", "币种列表：" + data);
                    CurrencyList currencyList = MyGson.getInstance().fromJson(data, CurrencyList.class);
                    if (currencyList.getType() == 1) {

                        currencyListData.addAll(currencyList.getData());
                        initCurrencyPopupWindow();
                        if (currencyList.getData().size() > 0) {
                            currencycode = currencyList.getData().get(0).getCode();
                            currencyId = currencyList.getData().get(0).getId();
                            GlideUtil.loadImage(PaySetDetailAc.this, NetHelperNew.URL + currencyListData.get(0).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                            tv_currrncyname.setText(currencyListData.get(0).getCode());
                            tv_cointype.setText("添加" + currencycode + "地址");
                            tv_addresstype.setText( currencycode + "地址");
                        }
                        findData(false);
                    } else {
                        loadingDialog.dismiss();
                        new ErrorDialog(PaySetDetailAc.this, currencyList.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(PaySetDetailAc.this, msg);
                }
            });


        }

    }

    /**
     * 查询子帐户结算地址
     *
     * @param isLoading
     */
    private void findData(boolean isLoading) {
        if (NetWorkUtil.check(this)) {
            if (isLoading)
                loadingDialog.show();
            NetHelperNew.ChildAccountAddress("" + currencyId, "" + getChildAccountID(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "查询子帐户结算地址：" + data);
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        if ("".equals(baseBean.getData())) {
                            ly_noset.setVisibility(View.VISIBLE);
                            ly_set.setVisibility(View.GONE);

                        } else {
                            ly_set.setVisibility(View.VISIBLE);
                            ly_noset.setVisibility(View.GONE);
                            tv_address.setText(baseBean.getData());
                        }
                    } else {
                        ToastUtil.showToast(PaySetDetailAc.this, baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(PaySetDetailAc.this, msg);
                }
            });

        }
    }

    /**
     * 币种选择pop初始化
     */
    private void initCurrencyPopupWindow() {
        currencyPopupWindow = new CurrencyPopupWindow(currencyListData, this, new CurrencyPopupWindow.OnSeleCurrencyLayoutListener() {
            @Override
            public void selectHouseLayout(int i) {
                currencyId = currencyListData.get(i).getId();
                currencycode = currencyListData.get(i).getCode();
                GlideUtil.loadImage(PaySetDetailAc.this, NetHelperNew.URL + currencyListData.get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                tv_currrncyname.setText(currencyListData.get(i).getCode());
                tv_cointype.setText("添加" + currencycode + "地址");
                tv_addresstype.setText( currencycode + "地址");
                findData(true);

            }
        });
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.fr_update, R.id.fr_stop, R.id.tv_verificationcode, R.id.ll_currency, R.id.btn_add, R.id.ll_finish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fr_update://修改钱包地址
                ReviseAddressAc.start(this,getChildAccountID(),currencyId+"",currencycode,tv_address.getText().toString().trim());

                break;
            case R.id.fr_stop://暂停支付
                StopPayDialog stopPayDialog = new StopPayDialog(this, R.style.dialog, "展厅支付");
                stopPayDialog.show();
                break;
            case R.id.tv_verificationcode:
                //获取验证码
                sendSMS();
                break;
            case R.id.ll_finish:

                finish();
                break;
            case R.id.ll_currency:
                //选择币种
                if (currencyPopupWindow != null)
                    currencyPopupWindow.showAsDropDown(ll_currency, 0, 0);
                break;
            case R.id.btn_add:
                //添加地址
                if (addCheck())
                    addAddress();
                break;
        }
    }

    private boolean addCheck() {
        if ("".equals(edit_address.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写钱包地址");
            return false;
        }
        if ("".equals(edit_code.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写短信验证码");
            return false;
        }
        return true;
    }

    /**
     * 添加地址
     */
    private void addAddress() {
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.ChangeChildAccountAddress("" + currencyId, "" + getChildAccountID(), edit_address.getText().toString().trim(), edit_code.getText().toString().trim(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "添加地址：" + data);
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        new SuccessDialog(PaySetDetailAc.this, "地址添加成功", new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                findData(true);
                            }
                        }).show();

                    } else {
                        ToastUtil.showToast(PaySetDetailAc.this, baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(PaySetDetailAc.this, msg);
                }
            });

        }


    }

    /**
     * 发送验证码
     */
    private void sendSMS() {
        if (NetWorkUtil.check(this)) {

            //倒计时
            timeCountUtil.start();


            NetHelperNew.ChangeChildAccountAddressSendSmsCode(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "修改密码" + baseBean.getContent());
                    if (baseBean.getType() == 1) {

                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    } else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==11)
        {
            findData(true);
        }
    }
}
